<?php
namespace app\api\validate;
use think\Validate;
use think\Db;

/**
 * Class UserValidate
 * @package app\api\validate
 * @author zhenHong~
 * 用户验证器
 */
class UserValidate extends Validate{
    protected $rule = [
        'pagenum'  => ['number'],
        'u_id'  => ['require','number'],
        'name'  => ['require'],
        'nickName' => ['require'],
        'phone' => ['require','regex' => '0?(13|14|15|17|18|19)[0-9]{9}','checkPhoneUnique'],
        'sex'   => ['require','number'],
        'head_pic' => ['require'],
        'lon' => ['require','between'=>'0,180'],
        'lat' => ['require','between'=>'0,90'],
        'listNum' => ['number'],
        'type' => ['checkType'],
        'start_time' => ['checkMyDate'],
        'end_time' => ['checkMyDate'],
        'code' => ['checkCode'],
        'nowtime' => ['require','number','checkNowTime'],
        'status' => ['in:0,1,9'],
        'content' => ['require']
    ];

    protected $message = [
        'nickName' => '昵称不能为空',
        'name' => '姓名不能为空',
        'u_id' => '用户id格式错误',
        'phone.require' => '手机号码格式错误',
        'phone.regex' => '手机号码格式错误',
        'phone.checkPhoneUnique' => '手机号码已被注册',
        'sex' => '性别参数错误',
        'head_pic' => '请上传头像',
        'lon' => '经度参数错误',
        'lat' => '纬度参数错误',
        'type' => '积分类型参数错误',
        'pagenum' => '分页参数错误',
        'listNum' => '记录条数参数错误',
        'start_time' => '开始时间参数错误',
        'end_time' => '结束时间参数错误',
        'code' => '验证码错误',
        'nowtime.number' => '时间参数错误',
        'nowtime.checkNowTime' => '当前已经签到',
        'status' => '信息状态参数错误',
        'content' => '请输入反馈内容'
    ];

    protected $scene = [
        //用户列表
        'selectUser'=>["pagenum"],
        //用户编辑个人信息
        'eidtUser' => ['u_id','name','nickName','sex','head_pic'],
        //用户签到
        'userSign' => ['u_id','lon','lat'],
        'userInfo' => ['u_id'],
        //积分列表
        'integrallist' => ['u_id','pagenum','listNum','u_id','type','start_time','end_time'],
        //更换手机号
        'changePhone' => ['phone','u_id','code'],
        //用户签到列表
        'signList' => ['u_id','pagenum','listNum'],
        //用户签到
        'judgeSign' => ['u_id','nowtime'],
        //添加反馈信息
        'addfeedback' => [
            'u_id',
            'content',
            'phone'=>['require','regex' => '0?(13|14|15|17|18|19)[0-9]{9}']
        ],
        //保养手册
        'mainmanual' =>[
            'u_id',
            'pagenum'
        ]
    ];

    /**
     * @param $value
     * @return bool
     * 检查积分类型参数
     */
    protected function checkType($value){
        if($value!=1 || $value!=2){
            return false;
        }else{
            return true;
        }
    }

    /**
     * @param $value
     * @return bool
     * 检查日期格式
     */
    protected function checkMyDate($value){
        if(date('Y-m-d H:i:s',strtotime($value)) == $value){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $value
     * @return bool
     * 更换手机号校验账号的唯一性
     */
    protected function checkPhoneUnique($value,$rule,$data){
        $where = [
            'u_id' => ['neq',$data['u_id']],
            'phone' => ['eq',$value]
        ];
        $result = Db::name('User')
            ->where($where)
            ->value('phone');
        return $result ? false : true;
    }

    /**
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     * 检查验证码
     */
    protected function checkCode($value,$rule,$data){
        if($value && $value == cache('phone'.$data['phone'])){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     * 检查当天是够有用户签到
     */
    protected function checkNowTime($value,$rule,$data){
        $date = date('Y-m-d',$value);
        $startTime = strtotime(date($date.' 00:00:00'));
        $endTime = strtotime(date($date.' 23:59:59'));
        $result = Db::name('Sign')
            ->where('id',$data['id'])
            ->whereBetween('ctime',"$startTime,$endTime")
            ->value('u_id');
        return $result ? true : false;
    }

}